#include "video.h"
#include <cstring>
Video::Video(long x, long y, char* path){

	cout << "estoy anets del python loco" << endl;
	//variables
	PyObject *packName, *pDict, *pX, *pY, *pTup, *pArgs;
	//otros
	PyObject *pModulo, *cVideo;
	Py_Initialize();

	cout << "estoy en python loco" << endl;
    // Importar modulos standard

	string instruction("sys.path.append(os.path.dirname(os.path.abspath(\"");
	instruction += path;
	instruction +="/py\")))";
	// armar los argumentos que le vamos a pasar al constructor d esta clase
	pX 		= PyInt_FromLong(x);
	pY 		= PyInt_FromLong(y);
	pTup 	= PyTuple_Pack(2, pX, pY);
	pArgs 	= PyTuple_Pack(1, pTup);

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("import time");
	PyRun_SimpleString(instruction.data());

    // Importar el modulo genVideo
	packName = PyString_FromString("genVideo");
    pModulo = PyImport_Import(packName);
    if(pModulo != NULL ){
        // Obetener dicciconario de namespace de este modulo
        pDict = PyModule_GetDict(pModulo);

        // obtener la clase Video de este modulo
        cVideo = PyDict_GetItemString(pDict, "Video");

        //pVideo es la clase que maneja el video
        this->pVideo 	= PyObject_CallObject(cVideo,pArgs);
    } else {
        this->pVideo = NULL;
    }
	return;
}

Video::Video(long x, long y){

	//variables
	PyObject *pNombre, *pDict, *pX, *pY, *pTup, *pArgs;
	//otros
	PyObject *pModulo, *cVideo;
	Py_Initialize();

    // Importar modulos standard

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("import time");
	PyRun_SimpleString("sys.path.append(os.getcwd())");

	// armar los argumentos que le vamos a pasar al constructor d esta clase
	pX 		= PyInt_FromLong(x);
	pY 		= PyInt_FromLong(y);
	pTup 	= PyTuple_Pack(2, pX, pY);
	pArgs 	= PyTuple_Pack(1, pTup);
    // Importar el modulo genVideo
	pNombre = PyString_FromString("genVideo");
    pModulo = PyImport_Import(pNombre);

    // Obetener dicciconario de namespace de este modulo
    if(pModulo != NULL){
        pDict = PyModule_GetDict(pModulo);
        // obtener la clase Video de este modulo
        cVideo = PyDict_GetItemString(pDict, "Video");

        //pVideo es la clase que maneja el video
        this->pVideo 	= PyObject_CallObject(cVideo,pArgs);

    } else {
        this->pVideo = NULL;
    }

	return;
}

bool Video::isOn() const{
    return pVideo != NULL;
}

void Video::pintar(const char* color){
    if(isOn()){
        char pintar[] = "pintar";
        char s[] = "s";
        PyObject_CallMethod(pVideo, pintar, s, color,NULL);
    }
	return;
}
Video::~Video(){
    if(isOn()){
        Py_DECREF(this->pVideo);
    }
	Py_Finalize();
}

void Video::refresh(){
    if(isOn()){
        char refresh[] = "refresh";
        PyObject_CallMethod(this->pVideo, refresh, NULL);
    }
}

int* Video::matriz(){
    int* res = NULL;
    if(isOn()){
        char array[] = "array";
        res = (int*)PyArray_DATA(PyObject_GetAttrString(this->pVideo, array));
    }
	return res;
}

void Video::waitForClick(int* pos){
	PyObject* pTup;
    if(isOn()){
        char waitForClick[] = "waitForClick";
        pTup =	PyObject_CallMethod(this->pVideo, waitForClick, NULL);
        pos[0] = (int)PyInt_AsLong(PyTuple_GetItem(pTup, (Py_ssize_t)0));
        pos[1] = (int)PyInt_AsLong(PyTuple_GetItem(pTup, (Py_ssize_t)1));
    }
	return;
}

void Video::getRect(int* pos){
	PyObject* pTup, *pIni, *pFini;
    if(isOn()){
        char waitForClick[] = "waitForClick";
        pTup =	PyObject_CallMethod(this->pVideo, waitForClick, NULL);
        pIni = PyTuple_GetItem(pTup, (Py_ssize_t)0);
        pFini = PyTuple_GetItem(pTup, (Py_ssize_t)1);
        pos[0] = (int)PyInt_AsLong(PyTuple_GetItem(pIni, (Py_ssize_t)0));
        pos[1] = (int)PyInt_AsLong(PyTuple_GetItem(pIni, (Py_ssize_t)1));
        pos[2] = (int)PyInt_AsLong(PyTuple_GetItem(pFini, (Py_ssize_t)0));
        pos[3] = (int)PyInt_AsLong(PyTuple_GetItem(pFini, (Py_ssize_t)1));
    }
	return;
}
